﻿//namespace estoque
//{
//    using System;
//    using System.Collections.Generic;
//    using System.Linq;
//    using System.Text;
//    using OtAut;

//    public class OmniTrackerHandler
//    {
//        private static List<string> listAdditionalInfos = new List<string>() { "Builddatum", "Programmversion", "Build" };

//        private string serverName;
//        private short port;
//        private string userId;
//        private string password;

//        private OtApplication otApp = null;
//        private OtSession otSession = null;
//        private OtUser currentUser = null;

//        public void InitOt()
//        {
//            // Omnitracker Automation Interface siehe:
//            // http://www.omninet.de/include/fileadmin/downloads/en/manuals/E_ai_manual_8_5.pdf
//            if (otSession == null && otApp == null)
//            {
//                otApp = new OtApplication();
//                otSession = otApp.MakeSession(this.serverName, this.port, this.userId, this.password);

//                foreach (OtUser user in otSession.Users)
//                {
//                    if (user.LoginName.Equals(this.userId, System.StringComparison.OrdinalIgnoreCase))
//                    {
//                        currentUser = user;
//                        //Console.WriteLine(currentUser.UniqueId);
//                        //Console.WriteLine(otSession.UserName);
//                    }
//                }
//            }
//        }

//        /// <summary>
//        /// Initializes a new instance of the <see cref="Exporter"/> class.
//        /// </summary>
//        /// <param name="serverName">Name of the server.</param>
//        /// <param name="port">The port.</param>
//        /// <param name="userId">The user id.</param>
//        /// <param name="password">The password.</param>
//        public OmniTrackerHandler(string serverName, int port, string password)
//        {
//            this.serverName = serverName;
//            this.port = (short)port;

//            // Console.WriteLine(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
//            this.userId = System.Environment.UserName.ToLowerInvariant();
//            //this.userId = userId;

//            this.password = password;

//            InitOt();
//        }

//        public List<OmniTrackerRequestQuickInfo> GetRequestList(string folderPath, bool? recursive, bool? filterUser)
//        {
//            List<OmniTrackerRequestQuickInfo> requestList = new List<OmniTrackerRequestQuickInfo>();
//            this.InitOt();
//            Console.WriteLine("Check: " + folderPath);
//            OtRequestFolder folder = otSession.GetRequestFolderByPath(folderPath);

//            //foreach (OtUserFieldDefinition def in folder.UserFieldDefinitions)
//            //{
//            //    System.Console.WriteLine(def.Label);
//            //}

//            OtFilter filter = folder.MakeFilter();

//            // get all OTs with the state !erledigt
//            filter.UserField["State"] = "erledigt";
//            filter.NegateField["State"] = true;

//            if (filterUser != null && filterUser == true)
//            {
//                // filter only for the current user
//                filter.set_UserField("Responsible", this.currentUser);
//            }

//            bool doRecursive = false;
//            if (recursive != null && recursive == true)
//            {
//                doRecursive = true;
//            }

//            OtRequests requests = folder.Search(filter, doRecursive);

//            int requestCount = requests.Count;

//            foreach (OtRequest req in requests) // folder.Requests
//            {
//                OmniTrackerRequestQuickInfo quickInfo = new OmniTrackerRequestQuickInfo()
//                {
//                    Title = req.Title,
//                    UniqueId = req.UniqueId,
//                    Number = req.Number
//                };

//                //OmniTrackerRequest singleRequest = GetSingleOT(req.UniqueId);
//                requestList.Add(quickInfo);
//            }

//            return requestList;
//        }

//        public List<OmnitrackerFolder> GetSubStructure(string folderPath)
//        {
//            List<OmnitrackerFolder> folderList = new List<OmnitrackerFolder>();

//            this.InitOt();

//            OtRequestFolder otFolder = otSession.GetRequestFolderByPath(folderPath);

//            foreach (OtRequestFolder subFolder in otFolder.SubFolders)
//            {
//                OmnitrackerFolder sub = new OmnitrackerFolder()
//                {
//                    FolderName = subFolder.Name,
//                    FolderPath = subFolder.Path,
//                    SubFolders = this.GetSubStructure(subFolder.Path)
//                };

//                folderList.Add(sub);
//            }

//            return folderList;
//        }

//        public OmniTrackerRequest GetSingleOT(int uniqueId)
//        {
//            this.InitOt();

//            // get the OTRequest by UniquId
//            OtRequest req = otSession.GetRequestByUniqueId(uniqueId);

//            StringBuilder sbAnswer = new StringBuilder();
//            foreach (OtAnswer answer in req.Answers)
//            {
//                sbAnswer.Append(answer.User.DisplayName);
//                sbAnswer.Append(" - ");
//                sbAnswer.Append(answer.Date.ToString());
//                sbAnswer.Append(Environment.NewLine);
//                sbAnswer.Append(answer.Text);
//                sbAnswer.Append(Environment.NewLine);
//                sbAnswer.Append("----------");
//                sbAnswer.Append(Environment.NewLine);
//            }

//            OmniTrackerRequest singleRequest = new OmniTrackerRequest()
//            {
//                UniqueId = req.UniqueId,
//                Number = req.Number,
//                Title = req.Title,
//                Prio = req.Priority.Name,
//                State = req.State.Name,
//                Description = req.Description,
//                CreationDate = req.CreationDate,
//                LastChanged = req.LastChange,
//                Applicant = req.Applicant.Name,
//                Responsible = req.Responsible.Name,
//                Application = req.Application.RegistryHostName + "/" + req.Application.RegistryPortNumber,
//                Class = req.Class.Name,
//                Answers = sbAnswer.ToString()
//            };

//            // Attachments
//            foreach (OtAttachment att in req.Attachments)
//            {
//                string contentBase64 = string.Empty;
//                if (!att.Link)
//                {
//                    contentBase64 = att.ContentBase64;
//                }

//                if (!singleRequest.Attachments.ContainsKey(att.Name))
//                {
//                    byte[] encodedDataAsBytes = System.Convert.FromBase64String(contentBase64);
//                    singleRequest.Attachments.Add(att.Name, encodedDataAsBytes);
//                }
//            }

//            // Additional Info of specific user fields.
//            foreach (OtUserField field in req.UserFields)
//            {
//                string label = field.Definition.Label;

//                if (listAdditionalInfos.Contains<string>(label))
//                {
//                    singleRequest.AdditionalInfos.Add(label, field.Value);
//                }
//            }

//            return singleRequest;
//        }

//        public void Logoff()
//        {
//            if (otApp != null && otSession != null)
//            {
//                // log off from the server
//                otSession.Logoff();
//            }
//        }
//    }
//}